package com.qidian.study.servlets;

import com.qidian.study.pojo.Content;
import com.qidian.study.service.ContentService;
import com.qidian.study.service.impl.ContentServiceImpl;
import com.qidian.study.util.DateFormat;
import com.qidian.study.util.JsonTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
@WebServlet("/content")
public class ContentServlet extends BaseServlet {
    private ContentService contentService = new ContentServiceImpl();
    public void add(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String text1 = request.getParameter("text1");
        String text2 = request.getParameter("text2");
        String pic1 = request.getParameter("pic1");
        String pic2 = request.getParameter("pic2");
        String lilnk1 = request.getParameter("link1");
        String lilnk2 = request.getParameter("link2");
        long catId = new Long(request.getParameter("catId"));
        Content c = new Content();
        c.setCatId(catId);
        c.setLink1(lilnk1);
        c.setLink2(lilnk2);
        c.setPic1(pic1);
        c.setPic2(pic2);
        c.setText1(text1);
        c.setText2(text2);
        c.setCreateTime(DateFormat.getNowString());
        c.setModifyTime(DateFormat.getNowString());
        c.setStatus(1);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(contentService.save(c)));
    }
    public void queryById(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        long contentId = new Long(request.getParameter("contentId"));
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(contentService.queryById(contentId)));
    }
    public void update(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        String text1 = request.getParameter("text1");
        String text2 = request.getParameter("text2");
        String pic1 = request.getParameter("pic1");
        String pic2 = request.getParameter("pic2");
        String lilnk1 = request.getParameter("lilnk1");
        String lilnk2 = request.getParameter("lilnk2");
        long catId = new Long(request.getParameter("catId"));
        long contentId = new Long(request.getParameter("contentId"));
        Content c = new Content();
        c.setCatId(catId);
        c.setLink1(lilnk1);
        c.setLink2(lilnk2);
        c.setPic1(pic1);
        c.setPic2(pic2);
        c.setText1(text1);
        c.setText2(text2);
        c.setCreateTime(DateFormat.getNowString());
        c.setModifyTime(DateFormat.getNowString());
        c.setStatus(1);
        c.setContentId(contentId);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(contentService.update(c)));
    }
    public void queryAll(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(contentService.queryAll()));
    }

    public void queryByCatId(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        synchronized (contentService) {
            Long catId = new Long(request.getParameter("catId"));
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(JsonTools.object2json(contentService.queryByCatId(catId)));
        }
    }
    public void queryByPage(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        int page = 1;
        int size = 10;
        try{
            page = new Integer(request.getParameter("page"));
        }catch (NumberFormatException e){}
        try{
            size = new Integer(request.getParameter("limit"));
        }catch (NumberFormatException e){}
        String text1 = request.getParameter("text1");
        long catId = 0;
        try{
            catId = new Long(request.getParameter("catId"));
        }catch (Exception e){}
        Content c = new Content();
        c.setText2(text1);
        c.setCatId(catId);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(contentService.queryByPage(page,size,c)));
    }
}
